package ATM;

public class Deposit extends Transaction implements ExecuteTransaction //inherits from Transaction superclass
{																	// implements ExecuteTransaction interface to execute the Factory pattern
	private double amount;
	private Keypad keypad;
	private DepositSlot depositSlot;
		
	public Deposit(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase,
			Keypad atmKeypad, DepositSlot atmDepositSlot)
	{
		super(userAccountNumber, atmScreen, atmBankDatabase);
		
		keypad = atmKeypad;
		depositSlot = atmDepositSlot;
	}
	
	@Override
	public void execute() // overrides execute() from the ExecuteTransaction interface
	{
		BankDatabase bankDatabase = getBankDatabase(); //any credit transaction is reflected in the Account of the user with the help of
		Screen screen = getScreen();					//an object of BankDatabase class. This is for security reason of the user account information
		amount = askDepositAmount();
		
		if(amount != 0)
		{
			screen.displayMessage("\nPlease insert the money in value of ");
			screen.displayDollarAmount(amount);
			screen.displayMessageLine(".");
			
			boolean cashReceived = depositSlot.isCashReceived(); //checks whether the DepositSlot class has received any money 
																//it will always return true, presuming the user has actually inserted the notes
			if(cashReceived)
			{
				screen.displayMessageLine("\nYour cash has been received." +
						"\nNOTE: The money just deposited will not be available " +
						"until\n we verify the amount of the cash you inserted.");
				
				bankDatabase.credit(getAccountNumber(), amount);
			}
			else
			{
				screen.displayMessageLine("\nYou did not insert any cash, " +
						"so the ATM has canceled your transaction.");
			}	
		}
		else
		{
			screen.displayMessageLine("\nCanceling transaction...");
		}
	}
	
	private double askDepositAmount()
	{
		Screen screen = getScreen();
		
		screen.displayMessage("\nPlease enter a deposit amount in CENTS (or 0 to cancel): ");
		int input = keypad.getInput();
		
		if(input == 0)
			return 0;
		else
		{
			return (double)input/100;
		}
	}
}